<?php

class AdminController extends Controller {

     public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('index','password','slide','slideAdd','slideDel','credit'),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    
    public function actionIndex() {
        $model = Config::model()->findByPk(1);

        if (isset($_POST['Config'])) {
            $model->attributes = $_POST['Config'];
            if ($model->save()){
                $this->redirect(array('index'));
            }                
        }

        $this->render('index', array(
            'model' => $model,
        ));
    }
    
    //修改管理員密碼
    public function actionPassword(){
        $model = Admin::model()->findByPk(1);
        $old = $model->password;

        if (isset($_POST['Admin'])) {
            $model->attributes = $_POST['Admin'];
            if(!$model->password){
                $model->password = $old;
            }else{
                $model->password = md5($model->password);
            }
            if ($model->save()){
                $this->redirect(array('password'));
            }                
        }
        
        $model->password='';

        $this->render('password', array(
            'model' => $model,
        ));
    }
    
    //首頁輪播管理
    public function actionSlide($class){
        if(!$class){
            $class==1;
        }
        switch ($class){
            case 1:
                $title = '首頁輪播';
                $size = '1000x570';
                break;
            case 2:
                $title = '活動廣告圖';
                $size = '1000x300';
                break;
            case 3:
                $title = '首頁推薦';
                $size = '302x240';
                break;
            case 4:
                $title = '商品廣告圖';
                $size = '740x250';
                break;
            default:
                $title = '首頁輪播';
        }
        $condition = new CDbCriteria();
        $condition->order='hot desc';
        $condition->addCondition('class='.$class);
        $rowTable = Slide::model()->findAll($condition);
        $this->render('slide', array(
            'rowTable' => $rowTable,
            'class'=>$class,
            'title'=>$title,
            'size' => $size,
        ));
    }
    
    //增加輪播
    public function actionSlideAdd($class){
        if(!$class){
            $class==1;
        }
        $this->layout = 'pop';
        $model = new Slide();
        if (isset($_POST['Slide'])) {
            $model->attributes = $_POST['Slide'];
            $pic = Helper::uploadPic($model,Yii::app()->params['slidePath']);
            if($pic){
                $model->pic = $pic;
            }
            $model->class=$class;
            if ($model->save()) {
                echo '<script>alert("operation successful!");parent.$.fancybox.close();</script>';
            }
        }
        $this->render('banner', array(
            'model' => $model,
            'class'=>$class,
            'size'=>$_GET['size'],
        ));
    }
    
    //刪除輪播
    public function actionSlideDel($id,$class){
        if(!$class){
            $class==1;
        }
        $model = Slide::model()->findByPk((int)$id);
        if($model && $model->delete()){
            echo '<script>alert("delete successful!");window.location.href="'.$this->createUrl('slide',array('class'=>$class)).'";</script>';
        }else{
            echo '<script>alert("error!");window.location.href="'.$this->createUrl('slide',array('class'=>$class)).'";</script>';
        }
    }
    
    //購物金明細
    public function actionCredit() {
        $search['s'] = $_REQUEST['s'];
        $search['e'] = $_REQUEST['e'];
        $search['username'] = $_REQUEST['username'];
        $search['cointype'] = $_REQUEST['cointype'];
        $search['coinsource'] = $_REQUEST['coinsource'];
        $search['coinstatus'] = $_REQUEST['coinstatus'];
        
        $condition = new CDbCriteria();
        $condition->with = 'user';
        
        if ($search['s']) {
            $condition->addCondition("t.dateline>='" . $search['s'] . "'");
        }
        if ($search['e']) {
            $condition->addCondition("t.dateline<'" . $search['e'] . "'");
        }
        if ($search['username']) {
            $condition->addCondition("user.username='" . $search['username'] . "'");
        }
        if ($search['cointype']) {
            $condition->addCondition("t.type='" . $search['cointype'] . "'");
        }
        if ($search['coinsource']) {
            $condition->addCondition("t.source='" . $search['coinsource'] . "'");
        }
        if ($search['coinstatus']) {
            $condition->addCondition("t.status='" . $search['coinstatus'] . "'");
        }
        
        
        $count = CoinItem::model()->count($condition);
        $pager = new CPagination($count);
        $pager->pageSize = 10;
        $pager->applyLimit($condition);
        $rowTable = CoinItem::model()->findAll($condition);
        unset($condition);
        $this->render('credit', array('rowTable' => $rowTable, 'pages' => $pager, 'search' => $search));
    }

}